package top.lixunda.ecommerce.server.good.api.entity.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import top.lixunda.commerce.common.core.exception.AppException;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.common.validate.annotation.XumIncludes;
import top.lixunda.common.validate.annotation.XumNotNull;
import top.lixunda.ecommerce.server.good.api.entity.db.PageInfoShowDO;

import java.util.List;

/**
 * 保存首页的Carousel数据DTO
 *
 * @author lixunda
 * @version time:2020/3/20 17:27
 */
@ApiModel("保存首页的Carousel数据DTO")
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class PageIndexCarouselInsertDTO {

    /**
     * 图片在左侧
     */
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public static final String DIRECTION_LEFT = "LEFT";
    /**
     * 图片在右侧
     */
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public static final String DIRECTION_RIGHT = "RIGHT";

    /**
     * 默认类型
     */
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public static final String TYPE_DEFAULT = "DEFAULT";
    /**
     * 图片类型，此时该轮播图页只是一张图片
     */
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public static final String TYPE_PICTURE = "PICTURE";

    @ApiModelProperty("数据类型")
    @XumNotNull(message = "数据类型不能为空")
    @XumIncludes({PageIndexCarouselInsertDTO.TYPE_DEFAULT,
            PageIndexCarouselInsertDTO.TYPE_PICTURE})
    private String type;

    @ApiModelProperty("大标题")
    @XumNotNull(message = "标题不能为空")
    private String title;

    @ApiModelProperty("顺序号")
    private Integer sequence;

    @ApiModelProperty("图片")
    @XumNotNull(message = "商品图片不能为空")
    private String image;

    @ApiModelProperty("图片文件编号")
    @XumNotNull(message = "商品图片文件编号不能为空")
    private Integer imageFileId;

    @ApiModelProperty("背景图片")
    @XumNotNull(message = "背景图片不能为空")
    private String backImage;

    @ApiModelProperty("背景图片文件编号")
    @XumNotNull(message = "背景图片文件编号不能为空")
    private Integer backImageFileId;

    @ApiModelProperty("主商品编号")
    @XumNotNull(message = "链接商品不能为空")
    private Integer linkGoodId;

    @ApiModelProperty("次商品编号列表")
    private List<Integer> goodIdList;

    @ApiModelProperty("展示方式，LEFT RIGHT")
    @XumNotNull(message = "展示方式不能为空")
    @XumIncludes({DIRECTION_LEFT, DIRECTION_RIGHT})
    private String direction;

    /**
     * 转换数据，将本DTO转换为PageInfoShowDO
     *
     * @return PageInfoShowDO
     * @see top.lixunda.ecommerce.server.good.api.entity.vo.PageCarouselVO
     */
    public PageInfoShowDO toPageInfoShowDO() {
        PageInfoShowDO pageInfoShowDO = new PageInfoShowDO()
                .setType(this.getType())
                .setSequence(this.getSequence())
                .setImage1(this.getImage())
                .setImageFile1(this.getImageFileId())
                .setImage2(this.getBackImage())
                .setImageFile2(this.getBackImageFileId())
                .setLink(String.valueOf(this.getLinkGoodId()))
                .setContent5(this.getTitle())
                .setContent6(this.getDirection());
        if (ObjectValidators.isEmpty(goodIdList)) {
            throw new AppException("次商品信息不能为空");
        }
        int size = goodIdList.size();
        if (size >= 1) {
            pageInfoShowDO.setContent1(String.valueOf(goodIdList.get(0)));
        }
        if (size >= 2) {
            pageInfoShowDO.setContent2(String.valueOf(goodIdList.get(1)));
        }
        if (size >= 3) {
            pageInfoShowDO.setContent3(String.valueOf(goodIdList.get(2)));
        }
        return pageInfoShowDO;
    }

}
